﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRM.Entity;
using HRM.Utility;
using HRM.BIZ;

namespace WebUI.Pages
{
    public partial class EditEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    int empID = id.ToInt32();
                    LoadEmployeeDetails(empID);
                }
                else
                    Response.Redirect("~/Default.aspx");
            }
        }

        private void LoadEmployeeDetails(int id)
        {
            Employee emp = new EmployeeBiz().GetByID(id);
            if (emp != null)
            {
                ucEmpDetails.CurrentEmployeeDetails = emp;
                ucEmpNavigate.RecordID = emp.ID;
                ucEmpNavigate.EmployeeName = emp.Name;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                Employee emp = ucEmpDetails.CurrentEmployeeDetails;
                EmployeeBiz empBiz = new EmployeeBiz();
                empBiz.Update(emp);
                ucEmpDetails.UploadEmployeePicture(emp.PictureLink);

                //Page.ClientScript.RegisterStartupScript(this.GetType(), "msgscript",
                  //  "<script language='javascript'>displayMessage('Cập nhật thành công.');</script>");
                DefaultMasterPage mp = this.Master as DefaultMasterPage;
                mp.RunStartupScript("displayMessage('Cập nhật thành công.')");
            }
            catch (Exception ex)
            {
                DefaultMasterPage mp = this.Master as DefaultMasterPage;
                mp.RunStartupScript("displayMessage('Có lỗi xảy ra.')");
            }
        }
    }
}
